package com.example.cloudcustomer.service;

import com.example.cloudcustomer.client.CustomerClient;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Project: spring-cloud
 * @Package: com.example.cloudcustomer.service
 * @Author: 壹陸捌
 * @Time: 2025 - 08 - 06 17 : 04
 * @Description: 业务层代码
 */

@Service
public class CustomerService {

    @Autowired
    private CustomerClient customerClient;

    // 使用 Resilience4j 的 @CircuitBreaker 注解
    // name: 与 resilience4j 配置中的实例名对应
    // fallbackMethod: 降级方法名
    @CircuitBreaker(name = "selectProvide", fallbackMethod = "selectProvideFallback")
    public String selectProvide(String mark) {
        System.out.println("============开始远程调用！！！========================");
        // 正常调用
        return customerClient.selectProvide(mark);
    }

    // 降级方法签名必须与原方法兼容 (相同参数，最后一个是 Throwable)
    public String selectProvideFallback(String mark, Throwable throwable) {
        System.out.println("============触发降级！！！========================");
        System.out.println("调用失败，触发降级。原因: " + throwable.getMessage());
        return "触发服务降级";
    }

}
